﻿/*
SmsBox .NET
Copyright (c) 2011 Jakub "Cubix651" Cisło - http://cubix.one.pl

License: X11 (MIT) See README file.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmsBox.NET
{
    public partial class Kontakty : Form
    {
        public Form1 mainForm;

        public Kontakty()
        {
            InitializeComponent();
        }

        private void btnDodaj_Click(object sender, EventArgs e)
        {
            int i;
            if (String.IsNullOrEmpty(txbNazwa.Text) || String.IsNullOrEmpty(txbNumer.Text) || (!int.TryParse(txbNumer.Text, out i)) || (txbNumer.Text.Length < 4))
            {
                MessageBox.Show("Wypełnij poprawnie wszystkie pola!");
                return;
            }
            lsvKontakty.Items.Add(new ListViewItem(new string[]{txbNazwa.Text, txbNumer.Text}));
            txbNazwa.Text = "";
            txbNumer.Text = "";
            txbNazwa.Focus();
        }

        private void lsvKontakty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && (lsvKontakty.SelectedItems.Count != 0))
                lsvKontakty.SelectedItems[0].Remove();
        }

        private void Kontakty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) btnDodaj_Click(null, null);
        }

        private void edytujToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lsvKontakty.SelectedItems.Count != 0)
            {
                txbNazwa.Text = lsvKontakty.SelectedItems[0].SubItems[0].Text;
                txbNumer.Text = lsvKontakty.SelectedItems[0].SubItems[1].Text;
                lsvKontakty.SelectedItems[0].Remove();
            }
        }

        private void usuńToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lsvKontakty.SelectedItems.Count != 0)
            {
                lsvKontakty.SelectedItems[0].Remove();
            }
        }

        private void Kontakty_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void lsvKontakty_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lsvKontakty.SelectedItems.Count != 0)
            {
                mainForm.tbxNrTel.Text = lsvKontakty.SelectedItems[0].SubItems[1].Text.Substring(3);
                for (int i = 0; i < mainForm.cmbNrTel.Items.Count; i++)
                {
                    if (mainForm.cmbNrTel.Items[i].ToString() == lsvKontakty.SelectedItems[0].SubItems[1].Text.Substring(0, 3))
                    {
                        mainForm.cmbNrTel.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
    }
}
